'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ChatGroupdetailCtrl
 * @description
 * # ChatGroupdetailCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ChatGroupdetailCtrl', function ($routeParams, Groups, ChatStatus, ChatUser, RongYService, $location, Util) {
        var vm = this;
        var targetId = $routeParams.id
        var param = {}
        param.groupId = targetId;
        var getGroupDetail = function () {
            RongYService.getGroup(param).then(function (data) {
                if (data) {
                    var owner = ChatUser.get(data.groupHead)
                    vm.groupDetail = data;
                    vm.groupMaster = owner && owner.name;
                } else {
                    removeConversation("该群组已经解散")
                }
            })
        };
        var getNotificationStatus = function () {
            RongYService.getConversationNotificationStatus(targetId, ChatStatus.getChatType()).then(function (data) {
                vm.checked = !!data.code
            })
        }
        this.quitGroup = function () {
            // 解散群组
            if (Util.isCurId(vm.groupDetail.groupHead)) {
                RongYService.dismissgroup(param).then(function (data) {
                    // 清除会话
                    removeConversation(data)
                    Groups.remove(targetId)
                })
            } else {
                // 退出群组
                RongYService.quitGroup(param).then(function (data) {
                    // 清除会话
                    removeConversation(data)
                    Groups.remove(targetId)
                })
            }
        }
        var removeConversation = function (msg) {
            var conversation = {
                conversationType: "GROUP",
                targetId: targetId
            };
            RongYService.removeConversation(conversation).then(function (data) {
                Util.Toast(msg, {
                    callback: function () {
                        $location.path("chat/index").replace();
                    }
                })
            })
        }
        this.discussonUsers = function (lists) {
            ChatStatus.setUsers(lists);
            $location.path("chat/groupusers/" + targetId + "/" + vm.groupDetail.groupHead);
        }
        this.switchNotice = function () {
            var notificationStatus = "";
            notificationStatus = vm.checked ? "DO_NOT_DISTURB" : "NOTIFY";
            RongYService.setConversationNotificationStatus(targetId, ChatStatus.getChatType(), notificationStatus).then(function (data) {
                vm.checked = !!data.code
            })
        };
        this.changeName = function () {
            if (navigator.notification && Util.isCurId(vm.groupDetail.groupHead)) {
                navigator.notification.prompt(
                    '修改群组名称', // message
                    vm.refreshgroup, // callback to invoke
                    '群组名称', // title
                    ['确定', '取消'] // buttonLabels
                );
            }
        };
        this.refreshgroup = function (results) {
            if (results.buttonIndex == 1) {
                if (!results.input1) {
                    Util.Toast("群名称不能为空");
                    return;
                }
                if (results.input1 > 7) {
                    Util.Toast("群名称不得超过7个字");
                    return;
                }
                ChatStatus.setName(results.input1);
                var param = {};
                param.groupId = targetId;
                param.groupName = results.input1;
                RongYService.refreshgroup(param).then(function (data) {
                    vm.groupDetail.groupName = results.input1;
                })
            }
        };
        var init = function () {
            vm.logo = ChatUser.GroupImg
            getGroupDetail()
            getNotificationStatus()
        }
        init();
    });
